/**
 * 
 */
package com.p2p.front.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.base.page.PageVo;
import com.p2p.front.component.MemberSetting;
import com.p2p.front.dal.dao.MemberPointDao;
import com.p2p.front.dal.dao.MemberPointLogDao;
import com.p2p.front.dal.model.MemberPoint;
import com.p2p.front.dal.model.MemberPointLog;
import com.p2p.front.dal.model.MemberPointLog.DirectionEnum;
import com.p2p.front.dal.model.MemberPointLog.TypeEnum;
import com.p2p.front.service.MemberPointService;
import com.p2p.front.service.SysConfigService;

/**
 * @author ruiqi
 *
 */
@Service("memberPointService")
public class MemberPointServiceImpl implements MemberPointService {

	private final Logger logger = LoggerFactory.getLogger(MemberPointServiceImpl.class);
	
	@Resource
	private MemberPointLogDao memberPointLogDao;
	
	@Resource
	private MemberPointDao memberPointDao;
	
	@Resource
	private SysConfigService sysConfigService;
	
	@Resource
	private MemberSetting memberSeting;
	
	
	@Transactional
	@Override
	public boolean cumulative(String userId,int point, TypeEnum type) {
		//必须开启事务，对用户积分数据加锁
		MemberPoint memberPoint = memberPointDao.selectForUpdateByUserId(userId);
		if(memberPoint==null){//没有就创建
			memberPoint = new MemberPoint(userId);
			memberPointDao.create(memberPoint);
			memberPoint = memberPointDao.selectForUpdateByUserId(userId);
		}
		boolean opResult = false;
		
		if(type.getDirection()==DirectionEnum.produce){//产生积分
			opResult = memberPoint.producePoint(point);
		}else if(type.getDirection()==DirectionEnum.consume){//消耗积分
			opResult = memberPoint.consumePoint(point);
		} else if(type.getDirection()==DirectionEnum.ChangeLev) {	//修改积分等级
			opResult = true;
		}
		//积分变更失败
		if(!opResult){
			return false;
		}
		//积分流水日志
		if(point!=0) {
			createLog(userId,point,type);
		}
		//检查是否触发会员等级提升
		checkLevelUpgrade(memberPoint);
		//更新积分及等级，之后立即提交事务
		memberPointDao.update(memberPoint);
		return true;
	}
	
	private void createLog(String userId,int point,TypeEnum type){
		MemberPointLog memberPointLog = new MemberPointLog(userId,point,type);
		memberPointLogDao.create(memberPointLog);
	}
	
	private void checkLevelUpgrade(MemberPoint memberPoint){
		Integer currentLevel = memberPoint.getLevel();
		Integer upgradeLevel = currentLevel+1;
		Integer levelNeedPoint = memberSeting.getMemberLevelPoint(upgradeLevel);
		if(levelNeedPoint!=null){
			if(memberPoint.getProduceCumulative().intValue()>=levelNeedPoint.intValue()){
				memberPoint.setLevel(upgradeLevel);
			}
		}
	}
	

	@Override
	public MemberPoint getUserMemberPoint(String userId) {
		MemberPoint memberPoint = memberPointDao.getByUserID(userId);
		if(memberPoint==null){
			memberPoint = new MemberPoint(userId);
			memberPointDao.create(memberPoint);
		}
		return memberPoint;
	}

	

	@Override
	public PageVo listPage(PageVo form) {
		List<Map<String, Object>> list = memberPointLogDao.list(form);
		form.setList(list);
		return form;
	}

	@Override
	public PageVo listAllUserPoint(PageVo form) {
		List<Map<String,Object>> list = memberPointDao.listAllUserPoint(form);
		form.setList(list);
		return form;
	}

	@Override
	public Map<String, MemberPoint> queryByUserIdList(List<String> userIdList) {
		Map<String, MemberPoint> memberPointMap = new HashMap<String, MemberPoint>();
		if(CollectionUtils.isNotEmpty(userIdList)){
			List<MemberPoint> list =  memberPointDao.queryByUserIdList(userIdList);
			for(MemberPoint memberPoint:list){
				memberPointMap.put(memberPoint.getUserId(), memberPoint);
			}
		}
		return memberPointMap;
	}

	@Override
	public void update(MemberPoint memberPoint) {
		memberPointDao.update(memberPoint);
	}

	
}
